<?php

//主页
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Console\EnvironmentCommand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class HomeController extends  Controller{


    //投诉
    public function complaints($id){
        $data        = DB::table('complaints')
                     ->leftjoin('user','complaints.user_complaints','=','user.user_id')
                     ->leftjoin('village','user.user_village','=','village.village_id')
                     ->where('village_id',$id)
                     ->where('operation_status',0)
                     ->count('operation_status');
        return $data;
    }

    //报修报事
    public function repair($id){
        $data        = DB::table('repair')
            ->leftjoin('user','repair.user_repair','=','user.user_id')
            ->leftjoin('village','user.user_village','=','village.village_id')
            ->where('village_id',$id)
            ->where('status',0)
            ->count('status');
        return $data;
    }

    //小区公告
    public function notice($id){
        $data        = DB::table('notice')
            //->leftjoin('user','notice.user_repair','=','user.user_id')
            //->leftjoin('village','notice.notice_village','=','village.village_id')
            ->where('notice_village',$id)
            ->skip(0)->take(1)
            ->orderBy('notice_id','DESC')
            ->get();
        return $data;
    }



    public function Home(Request $request){
        if($request ->isMethod('post')){
            $village_id        = $request ->input('village_id');
            $complaints        = $this->complaints($village_id);
            $repair            = $this ->repair($village_id);
            $notice            = $this ->notice($village_id);
            foreach ($notice as $value){
                $notice_title      = $value ->notice_title;
            }
            $da                = array(
                'complaints_sum'   =>$complaints,
                'repair_sum'            =>$repair,
                'title'             =>$notice_title
            );

                return json::json('success', '获取全部数据成功', $da);

        }
    }

    public function ClickStart(Request $request,$type){
        if($request  ->isMethod('post')){
            $complaints_id = $request->input('id');
            if($type ==1) {

                $data = DB::table('complaints')
                    ->where('complaints_id', $complaints_id)
                    ->update([
                        'operation_status' => 1
                    ]);
                if ($data == true) {
                    return json::json('success', '信息已经被读取', '');
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }else{
                $data        = DB::table('repair')
                    ->where('repair_id', $complaints_id)
                    ->update([
                        'status_option' => 1
                    ]);

                if ($data == true) {
                    return json::json('success', '信息已经被读取', '');
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', '');
                }
            }
        }
    }


}